/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logica;

import java.io.*;
import javax.swing.*;
import javax.swing.text.StyledEditorKit;
import jxl.write.*;
import jxl.*;

public class ExcelJTableExporter {

    private File f;
    //private File file;
    private JTable table;
    private String nombreTab;

    public ExcelJTableExporter(JTable table, String nombreTab) {
        //this.file = file;
        this.table = table;
        this.nombreTab = nombreTab;
    }

    public File saveFile() {
        JFileChooser fc = new JFileChooser();
        int option = fc.showSaveDialog(null);
        if (option == JFileChooser.APPROVE_OPTION) {
            String filename = fc.getSelectedFile().getName();
            String path = fc.getSelectedFile().getParentFile().getPath();

            int len = filename.length();
            String ext = "";
            String file = "";

            if (len > 4) {
                ext = filename.substring(len - 4, len);
            }

            if (ext.equals(".xls")) {
                file = path + "\\" + filename;
                File fw = new File(file);
                f = fw;
            } else {
                file = path + "\\" + filename + ".xls";
                File fw = new File(file);
                f = fw;
            }
        }
        return f;

    }

    public boolean export() {

        File file = saveFile();

        try {
            DataOutputStream out = new DataOutputStream(new FileOutputStream(file));

            WritableWorkbook w = Workbook.createWorkbook(out);

            WritableSheet s = w.createSheet(nombreTab, 0);
            Object objeto;
            WritableFont wf = new WritableFont(WritableFont.ARIAL,
                    10, WritableFont.BOLD);
            WritableCellFormat cf = new WritableCellFormat(wf);
            // cf.setWrap(true);
            int anchoCelda;

            for (int i = 0; i < table.getColumnCount(); i++) {

                objeto = table.getColumnName(i);
                s.addCell(new Label(i, 0, String.valueOf(objeto), cf));
                anchoCelda = objeto.toString().length() + 3;

                for (int j = 1; j < table.getRowCount(); j++) {
                    objeto = table.getValueAt(j, i);
                    String l = String.valueOf(objeto);

                    s.addCell(new Label(i, j, String.valueOf(objeto)));
                    if (anchoCelda > l.length()) {
                        s.setColumnView(i, anchoCelda);

                    } else {
                        s.setColumnView(i, l.length() + 3);
                    }
                }
            }

            w.write();
            w.close();
            out.close();

            JOptionPane.showMessageDialog(null, "Datos exportados con exito", "Exportar Excel", JOptionPane.INFORMATION_MESSAGE, null);
            return true;

        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (WriteException ex) {
            ex.printStackTrace();
        }
        return false;
    }
}
